package com.zds.hj.poseidon.biz.dispatcher.credit;

import com.zds.boot.common.exception.BusinessException;
import com.zds.boot.dispatch.annotation.DispatcherHandler;
import com.zds.boot.dispatch.comm.enums.ModuleNodeTypeEnum;
import com.zds.boot.dispatch.dto.BaseReqModel;
import com.zds.boot.dispatch.dto.BaseResultModel;
import com.zds.boot.dispatch.dto.InstructDto;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.dao.CreditInfoEntityDao;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.entity.CreditInfoEntity;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.CreditApplyService;
import com.zds.hj.poseidon.biz.cba.hpp.domain.credit.service.layout.ProductQueryService;
import com.zds.hj.poseidon.facade.dto.hpp.credit.CreditInfoEntityDto;
import com.zds.hj.product.facade.constant.ApplicationContainerConstant;
import com.zds.zly.common.utils.EmptyChecker;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static com.zds.zly.common.enums.HjResultCodeEnum.PARAMS_VALIDATE_FAIL;


/**
 * @Author wex
 * @Description 授信融资申请
 * @Date 2023-06-08 15:54
 */
@DispatcherHandler(value = "CreditInvoiceIntentionHandler", desc = "授信融资申请", applicationContainer = ApplicationContainerConstant.FRONT_END, nodeType = ModuleNodeTypeEnum.BUSINESS)
@Slf4j
public class CreditInvoiceIntentionHandler extends CreditCheckAbsHandler {


    @Autowired
    private CreditInfoEntityDao creditInfoEntityDao;
    @Resource
    private CreditApplyService creditApplyService;

    @Resource
    private ProductQueryService productQueryService;

    /**
     * 回显信息
     * @param params
     * @return
     */
    @Override
    public Object echoDetail(BaseReqModel params) {
        //获取数据库信息
        CreditInfoEntityDto dbData = findCreditApply(params);
        Map<String, Object> echoMapInfo = productQueryService.getProductConfigParams(Long.valueOf(params.getProductId()));
        echoMapInfo.put("daData", dbData);
        return echoMapInfo;
    }

    private CreditInfoEntityDto findCreditApply(BaseReqModel params) {
        //获取数据库信息
        CreditInfoEntityDto creditInfoEntityDto = creditApplyService.findCreditOnTransit(params);
        return creditInfoEntityDto;
    }

    @Override
    public void creditDoExecute(BaseReqModel params, BaseResultModel resultModel, CreditInfoEntityDto reqData, CreditInfoEntityDto dbData) {
        log.info("借款信息入参:{}", reqData);
        if(reqData.getApplicationsAmount() > 2000000) {
            throw new BusinessException("申请金额不能超过2000000元", PARAMS_VALIDATE_FAIL.code());
        }

        dbData.setApplyCreatorName(reqData.getApplyCreatorName());
        dbData.setApplicationsAmount(reqData.getApplicationsAmount());
        dbData.setLoanTerm(reqData.getLoanTerm());
        dbData.setRepaymentMethod(reqData.getRepaymentMethod());
        dbData.setRepaymentCycle(reqData.getRepaymentCycle());
        dbData.setLoanPurpose(reqData.getLoanPurpose());

    }

    @Override
    public Object paramsFerry(Object o) {
        CreditInfoEntityDto dbDto = (CreditInfoEntityDto) o;
        CreditInfoEntityDto creditInfoEntityDto = new CreditInfoEntityDto();
        creditInfoEntityDto.setApplyCreatorName(dbDto.getApplyCreatorName());
        creditInfoEntityDto.setApplicationsAmount(dbDto.getApplicationsAmount());
        creditInfoEntityDto.setLoanTerm(dbDto.getLoanTerm());
        creditInfoEntityDto.setRepaymentMethod(dbDto.getRepaymentMethod());
        creditInfoEntityDto.setRepaymentCycle(dbDto.getRepaymentCycle());
        creditInfoEntityDto.setLoanPurpose(dbDto.getLoanPurpose());
        return creditInfoEntityDto;
    }
}
